<?php

/**
 * Created by PhpStorm.
 * User: xiaoqiang
 * Date: 15-3-23
 * Time: 上午9:41
 */
class LibConfig
{
    static private $_config = array();

    /**
     * 读取配置文件
     * @param $key
     * @param $field
     * @return array|bool
     */
    static private function _read($key, $field)
    {
        if (\LibFunction::isSinaSae()) {
            $key = 'sae';
        } else {
            $key || $key = "base";
        }
        $field || $field = "DB";
        if (!isset(self::$_config[$key])) {
            $cFile = SYS_CONFIG . "cnf.{$key}.php";
            if (!file_exists($cFile)) {
                return \LibLog::custom("config file not exists : {$cFile}");
            }
            self::$_config[$key] = include_once($cFile);
        }
        return self::$_config[$key][strtoupper($field)] ?: array();
    }

    /**
     * 读取DB信息
     * @param $key
     * @return array|bool
     */
    static public function readDB($key = "")
    {
        return self::_read($key, "DB");
    }

    /**
     * 读取Redis信息
     * @param $key
     * @return array|bool
     */
    static public function readRedis($key = "")
    {
        return self::_read($key, "REDIS");
    }

    /**
     * 读取Memcache信息
     * @param $key
     * @return array|bool
     */
    static public function readMemcache($key = "")
    {
        return self::_read($key, "MEMCACHE");
    }

    /**
     * 读取日志信息
     * @param $key
     * @return array|bool
     */
    static public function readLog($key = "")
    {
        return self::_read($key, 'LOG_DIR') ?: "base.log";
    }

    /**
     * 读取指定配置文件
     * @param $key
     * @param $field
     * @return array|bool
     */
    static public function read($key, $field)
    {
        if (!$key || !$field) {
            return [];
        }
        return self::_read($key, $field);
    }
}